package test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.xiaoleilu.hutool.io.IoUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.StrUtil;

import util.MacEcbUtils;
import util.TLVUtil;


public class Main {
	public static void main(String[] args) throws IOException, Exception {
//		for (int i = 0; i < 10; i++) {
//			send();
//		}
		
		ChinaUmsZjMessage msg = new ChinaUmsZjMessage();
		// 1.获取参数
		
		msg.AddBody(new TLV(2, "1", TLV.MetaType.C));
		msg.AddBody(new TLV(3, "8236", TLV.MetaType.C));
		Date dd = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String s=sf.format(dd);
		System.out.println("请求流水号："+s);
		msg.AddBody(new TLV(4, s, TLV.MetaType.C));
		msg.AddBody(new TLV(5, s, TLV.MetaType.C));

		msg.AddBody(new TLV(6, ChinaUMSZjConfig.chanid, TLV.MetaType.C)); // 渠道商编码
		msg.AddBody(new TLV(7, ChinaUMSZjConfig.chanterm, TLV.MetaType.C)); // 渠道商终端
		msg.AddBody(new TLV(8, ChinaUMSZjConfig.chanpass, TLV.MetaType.C)); // 渠道密码

		msg.AddBody(new TLV(17, "6217850800009760776", TLV.MetaType.C)); // 银行账号或卡号,压缩不足位时后补F
		msg.AddBody(new TLV(31, ChinaUMSZjConfig.chanid, TLV.MetaType.C)); // 渠道端的商户号
		msg.AddBody(new TLV(32, ChinaUMSZjConfig.chanterm, TLV.MetaType.C)); // 渠道端的商户终端号
		
		String NM = TLV.createTLV("NM", "冯智豪");
		String MPN = TLV.createTLV("MPN", "15021303239");
		String ID = TLV.createTLV("ID", "411328199006130018");
		String _60TLV=StrUtil.join("" 
				,NM
				,MPN
				,ID
				);
		System.out.println("_60TLV:"+_60TLV);
		msg.AddBody(new TLV(60, _60TLV, TLV.MetaType.C)); // 附加字段
		TLVUtil.sendMessage(msg);
		HashMap<Integer, TLV> responseMap = msg.getMsgMapRes();
		System.out.println();
		for (Integer c : responseMap.keySet()) {
			TLV t=responseMap.get(c);
			byte[] gbd = t.getByteData();
			StringBuilder sbb=new StringBuilder();
			for ( int i = 0; i < gbd.length; i++) {
				if ( Integer.toHexString(0xFF & gbd[i]).length() == 1 ) {
					sbb.append( "0" + Integer.toHexString(0xFF & gbd[i]).toUpperCase() );
				} else {
					sbb.append( Integer.toHexString(0xFF & gbd[i]).toUpperCase() );
				}
			}
			System.out.println(c+"\t"+sbb+"\t\t\t"+t.getValue());
		}
	}
}
